 // 判断是否登录
 window.onload = function(){
    ajax({
        url: './php/index_user_session.php',
        data: {},
        method: 'post',
        success: function(res){
            //console.log(res);
            var res = JSON.parse(res);
            if(res.id == 1){ // 登录成功
                // 监听导航栏
                navUserFn(res.login_name);

                // 防止刷新显示"欢迎回来"
                window.sessionStorage.setItem('power', JSON.stringify(res.data));
                var flagId = window.sessionStorage.getItem('flagId');
                if(!flagId){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.msg('欢迎回来'+res.login_name, {
                            offset: '100px'
                        });   
                    });
                    window.sessionStorage.setItem('flagId', 'flagId');
                }
            }else if(res.id == 0){ // 尚未登陆
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.alert('您尚未登陆, 请先登录!',{
                        shade: [1, '#fff']
                    }, function(index){
                    //do something
                        location.href = './login.html';
                        layer.close(index);
                    });    
                });
            }else{ // error
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.alert('访问失败, 请联系相关人员',{
                        shade: [1, '#fff']
                    }, function(index){
                    //do something
                        location.href = './login.html';
                        layer.close(index);
                    });    
                });
            }
        }
    });
}

function navUserFn(name){
    var alyNav = document.getElementById('aly-nav'),
        navAdmiName = alyNav.querySelector('.nav-admi-name');
    navAdmiName.innerHTML = name;
    //alert(1)
    layui.use('element', function(){
    var element = layui.element;
    //一些事件监听
    element.on('nav(navUser)', function(data){

        /* 退出操作 */
        var logout = this.attributes['data-logout'];
        if(logout){
            console.log(1)
            // 添加加载动画
            var index2 = layer.load(2, {time: 10*1000});
            ajax({
                url: './php/index_logout.php',
                data: {login_name: name},
                method: 'post',
                success: function(res){
                    //关闭
                    layer.close(index2);     
                    if(res == 1){
                        layer.alert('退出成功', function(){
                            location.href = "login.html";
                        });
                    }else{
                        layer.alert('退出失败, 请联系相关人员');
                    }

                }
            });
        }

        /* 修改密码 */
        var dataClick = this.attributes['data-click'];
        if(!dataClick) return;
            //console.log(1)
            var title = this.innerHTML,
                content = this.attributes['lay-href'].value,
                id = this.attributes['data-id'].value;

            var layuiTabTitle = document.querySelector('.layui-tab-title');
            // 如果为true 说明tab存在, 反之则不存在
            var flag = false;
            for(var i=0, len=layuiTabTitle.children.length; i<len; i++){
                if(id == layuiTabTitle.children[i].attributes['lay-id'].value){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                element.tabAdd('demo', {
                    title: title,
                    content: `<iframe src="${content}"></iframe>`,
                    id: id
                });
            }
            //通过id切换到指定的Tab
            element.tabChange('demo', id);

        //console.log(data);
    });

    });
}